package com.liushuai.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liushuai.demo.entity.Demo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@ComponentScan(basePackages = "com.liushuai.demo")
@ContextConfiguration
@Controller
public class DemoController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @GetMapping("/")
    public @ResponseBody
    String index() {
        return "Start UP ..";
    }

    @GetMapping("/index")
    public String index(ModelMap modelMap, @RequestParam String code) {

        modelMap.put("code", code);
        modelMap.put("name", "和讯");

        return "index";
    }

    @PostMapping("/add")
    @ApiOperation(value = "保存数据测试")
    public @ResponseBody
    Map<String, Object> add(@RequestBody Demo demo) throws JsonProcessingException {
        Map<String, Object> map = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper(); //转换器

        //测试01：对象--json
        String json = mapper.writeValueAsString(demo);
        log.info(" ** 接收到请求信息：{}", json);
        demo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        try {
            //demoService.insert(demo);
            map.put("resp_code", "T");
        } catch (Exception e) {
            map.put("resp_code", "F");
            e.printStackTrace();
        }
        return map;
    }
}
